<template>
	<view class="add_assistant">
		<view class="search">
			<view class="search_left">
				<image src="@/static/home/search.png"></image>
				<input type="text" v-model="keyword" placeholder="Search" />
			</view>
			<view class="search_right flex-center" @click.stop="handleSearch(true)">Search</view>
		</view>
		<ul v-if="list.length>0">
			<p>Results</p>
			<li v-for="(item,index) in list" :key="index">
				<dl>
					<dt>
						<image :src="item.headimgurl||'/static/avatar.png'" mode=""></image>
					</dt>
					<dd>
						<view class="result_title">
							<text>{{item.name}}</text>
							<image v-if="!item.selected" @click.stop="handleSelected(item,index)"
								src="/static/business/jia.png" mode=""></image>
							<image v-else @click.stop="handleSelected(item,index)" src="/static/business/check_gray.png"
								mode=""></image>
						</view>
						<p>{{item.email}}</p>
						<p>{{item.phone}}</p>
						<p style="color: #888888;">Registration time: {{item.ctime}}</p>
					</dd>
				</dl>
			</li>
			<u-loadmore v-if="hasMore" :status="loadStatus" />

		</ul>
		<view class="empty" v-else>
			<image src="@/static/list.png" mode=""></image>
			<p>No user available</p>
		</view>
	</view>
</template>

<script>
	import {
		getSearchAssistant,
		getAddAssistant
	} from "@/lib/business";
	import {
		showFailure
	} from '@/lib/helper';
	export default {
		data() {
			return {
				keyword: "",
				loadStatus: 'loadmore',
				pageNo: 1,
				pageSize: 10,
				list: [],
				hasMore: false,
			}
		},
		methods: {
			handleSearch(isReset) {
				let _this = this;
				this.list = []
				this.pageNo = 1;
				if (!this.keyword) return;
				this.loadStatus = 'loading';
				let obj = {
					pageNo: _this.pageNo,
					pageSize: _this.pageSize,
					keyword: _this.keyword
				}
				getSearchAssistant(obj).then((res) => {
						if (res.statusCode == 0) {
							uni.hideLoading();
							this.loadStatus = 'no-more';
							this.hasMore = res.data.hasMore;
							if (isReset) {
								this.list = res.data.records;
							} else {
								this.list = this.list.concat(res.data.records);
							}
							this.list.forEach((item, index) => {
								item.selected = false;
							})
						}
					})
					.catch((error) => {
						this.loadStatus = 'no-more';
						uni.hideLoading();
						showFailure();
					});
			},
			handleSelected(item, index) {
				let _this = this;
				if (!item.selected) {
					getAddAssistant({
							userid: item.userid
						}).then((res) => {
							if (res.statusCode == 0) {
								uni.showToast({
									title: 'Added successfully',
									icon: 'none'
								})
								_this.list[index].selected = true;
							}else{
								uni.showToast({
									title: res.message,
									icon: 'none'
								})
							}
						})
				}else{
					uni.showToast({
						title: 'The user has already been added',
						icon: 'none'
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.search {
		height: 96rpx;
		background: #FFFFFF;
		border-radius: 50rpx;
		margin: 30rpx;
		margin-top: 50rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		background: #ECECEB;

		.search_left {
			display: flex;
			align-items: center;

			image {
				width: 40rpx;
				height: 40rpx;
				margin-left: 28rpx;
				margin-right: 22rpx;
			}
		}

		.search_right {
			width: 186rpx;
			height: 84rpx;
			background: #FC721A;
			border-radius: 50rpx;
			margin-right: 6rpx;
			font-weight: 500;
			font-size: 30rpx;
			color: #FFFFFF;
		}
	}

	ul {
		padding: 0 30rpx;
		margin-top: 55rpx;

		>p {
			font-weight: bold;
			font-size: 36rpx;
			color: #1C120D;
			line-height: 46rpx;
		}

		li {
			margin-top: 24rpx;

			dl {
				display: flex;

				dt {
					image {
						width: 120rpx;
						height: 120rpx;
						border-radius: 50%;
						margin-right: 30rpx;
						background: #eeeeee;
					}
				}

				dd {
					flex: 1;

					.result_title {
						display: flex;
						align-items: center;
						justify-content: space-between;
						font-weight: 500;
						font-size: 32rpx;
						color: #1C120D;

						image {
							width: 40rpx;
							height: 40rpx;
						}
					}

					>p {
						font-weight: 400;
						font-size: 24rpx;
						color: #444444;
						line-height: 30rpx;
					}
				}
			}
		}
	}

	.empty {
		text-align: center;
		margin-top: 10vh;

		image {
			width: 376rpx;
			height: 376rpx;
		}

		p {
			font-size: 28rpx;
			color: #999999;
		}
	}
</style>